package com.example.jetpacktest.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.jetpacktest.domain.Student
import com.example.jetpacktest.repository.RetrofitTest1Repository
import kotlinx.coroutines.launch
import java.lang.Exception

class RetrofitTest1ViewModel: ViewModel() {

    private val repository: RetrofitTest1Repository = RetrofitTest1Repository()

    val student: MutableLiveData<Student> = MutableLiveData()

    fun getStudent(id: Long) {

        viewModelScope.launch {
            try{
                student.value = repository.getStudent(id)
            }catch (ex: Exception){
                student.value = Student("未找到用户", 0)
                ex.printStackTrace()
            }
        }
    }

}